<template>
  <div class="dashboard-container">
    <div class="app-container">
      <table-tool>
        <template #right>
          <el-button type="primary" size="small" @click="addBtn('')">添加权限</el-button>
        </template>
      </table-tool>
      <!-- 表格 -->
      <el-table
        :data="permissionList"
        style="width: 100%;margin-bottom: 20px;"
        row-key="id"
        border
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      >
        <el-table-column
          prop="name"
          label="名称"
          width="180"
        />
        <el-table-column
          prop="code"
          label="标识"
          width="180"
          align="center"
        />
        <el-table-column
          prop="description"
          label="描述"
          align="center"
        />
        <el-table-column
          label="操作"
          width="180"
          align="center"
        >
          <template slot-scope="scope">
            <el-button v-if="scope.row.pid==='0'" type="text" size="small" @click="addBtn(scope.row)">添加</el-button>
            <el-button type="text" size="small" @click="editBtn(scope)">编辑</el-button>
            <el-button type="text" size="small" @click="delpermission(scope.row)">删除</el-button>
          </template>

        </el-table-column>
      </el-table>

      <!-- 添加编辑弹框 -->
      <el-dialog
        :title="type"
        :visible.sync="dialogVisible"
        width="50%"
        @close="handleClose"
      >
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
          <el-form-item label="名称" prop="name">
            <el-input v-model="ruleForm.name" />
          </el-form-item>
          <el-form-item label="标识" prop="code">
            <el-input v-model="ruleForm.code" />
          </el-form-item>
          <el-form-item label="描述" prop="description">
            <el-input v-model="ruleForm.description" />
          </el-form-item>
          <el-form-item label="状态" prop="enVisible">
            <el-switch v-model="ruleForm.enVisible" active-color="#13ce66" inactive-color="#ff4949" />
          </el-form-item>
        </el-form>
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button @click="dialogVisible = false">取消</el-button>
            <el-button type="primary" @click="handleConfirm">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermissionList, delPermissionById, addPermission, editPermission } from '@/api/permission'
import { tranListToTreeData } from '@/utils/index' // 转换树形结构
export default {
  data() {
    const validateName = async(rule, value, callback) => {
      const { data } = await getPermissionList()
      if (this.type === '新增权限') {
        data.some(item => item.name === value) ? callback(new Error('权限名称已存在')) : callback()
      } else {
        data.filter(item => item.name !== this.ruleForm.name).some(item => item.name === value) ? callback(new Error('权限名称已存在')) : callback()
      }
    }
    const validateCode = async(rule, value, callback) => {
      const { data } = await getPermissionList()
      if (this.type === '新增权限') {
        data.some(item => item.code === value) ? callback(new Error('权限标识已存在')) : callback()
      } else {
        data.filter(item => item.code !== this.ruleForm.code).some(item => item.code === value) ? callback(new Error('权限标识已存在')) : callback()
      }
    }
    return {
      permissionList: [], // 权限列表
      dialogVisible: false, // 添加编辑弹框
      type: '', // 添加编辑弹框标题
      ruleForm: {
        name: '',
        code: '',
        description: '',
        enVisible: '',
        pid: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { validator: validateName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入标识', trigger: 'blur' },
          { validator: validateCode, trigger: 'blur' }
        ]
      }

    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    // 获取权限列表
    async getPermissionList() {
      const { data } = await getPermissionList()
      this.permissionList = tranListToTreeData(data, '0')
    },
    // 删除权限
    delpermission(row) {
      this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delPermissionById(row.id)
        if (res.success) {
          this.$message({
            type: 'success',
            message: res.message
          })
          this.getPermissionList()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 添加权限btn
    addBtn(status) {
      this.type = '新增权限'
      this.dialogVisible = true
      if (status) {
        this.ruleForm.pid = status.id
      } else {
        this.ruleForm.pid = '0'
      }
    },
    // 编辑权限btn
    editBtn(scope) {
      this.type = '编辑权限'
      this.dialogVisible = true
      this.ruleForm = scope.row
      console.log(this.ruleForm)
    },
    handleConfirm() {
      this.$refs.ruleForm.validate(async(valid) => {
        if (valid) {
          if (this.type === '新增权限') {
            const res = await addPermission(this.ruleForm)
            if (res.success) {
              this.$message({
                type: 'success',
                message: res.message
              })
            }
          } else {
            const res = await editPermission(this.ruleForm.id, this.ruleForm)
            if (res.success) {
              this.$message({
                type: 'success',
                message: res.message
              })
            }
          }
          this.dialogVisible = false
          this.getPermissionList()
        } else {
          return false
        }
      })
    },
    // 清空表格
    clearForm() {
      this.ruleForm = {
        name: '',
        code: '',
        description: '',
        enVisible: '',
        pid: ''
      }
    },
    // 关闭弹框
    handleClose() {
      this.clearForm()
      this.$refs.ruleForm.clearValidate()
    }
  }
}

</script>

<style lang="scss" scoped>

</style>
